package com.livew.framework.generator;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;

public class MySelectByIdListElementGenerator extends AbstractXmlElementGenerator {

	public MySelectByIdListElementGenerator() {
		super();
	}

	@Override
	public void addElements(XmlElement parentElement) {
		XmlElement answer = new XmlElement("select"); //$NON-NLS-1$

        answer.addAttribute(new Attribute(
                "id", "selectByIdList")); //$NON-NLS-1$
        if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
            answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
                    introspectedTable.getResultMapWithBLOBsId()));
        } else {
            answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
                    introspectedTable.getBaseResultMapId()));
        }

        answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
                "java.lang.Long"));
        context.getCommentGenerator().addComment(answer);
        StringBuilder sb = new StringBuilder();
        sb.append("select "); //$NON-NLS-1$

        if (stringHasValue(introspectedTable
                .getSelectByPrimaryKeyQueryId())) {
            sb.append('\'');
            sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
            sb.append("' as QUERYID,"); //$NON-NLS-1$
        }
        answer.addElement(new TextElement(sb.toString()));
        answer.addElement(getBaseColumnListElement());
        if (introspectedTable.hasBLOBColumns()) {
            answer.addElement(new TextElement(",")); //$NON-NLS-1$
            answer.addElement(getBlobColumnListElement());
        }

        sb.setLength(0);
        sb.append("from "); //$NON-NLS-1$
        sb.append(introspectedTable
                .getAliasedFullyQualifiedTableNameAtRuntime());
        answer.addElement(new TextElement(sb.toString()));
        answer.addElement(new TextElement(" where is_del=0 "));
        sb.setLength(0);
        sb.append(" and id in ");
        answer.addElement(new TextElement(sb.toString()));  
        sb.setLength(0);
		sb.append("<foreach collection=");
		sb.append("\"list");
		sb.append("\" index=");
		sb.append("\"index");
		sb.append("\" item=");
		sb.append("\"item");
		sb.append("\" open=");
		sb.append("\"(");
		sb.append("\" separator=");
		sb.append("\",");
		sb.append("\" close=");
		sb.append("\")");
		sb.append("\">");
		answer.addElement(new TextElement(sb.toString()));   
		sb.setLength(0);
		sb.append(" #{item} ");
	    answer.addElement(new TextElement(sb.toString()));
	    sb.setLength(0);
		sb.append("</foreach>");
	    answer.addElement(new TextElement(sb.toString()));
        if (context.getPlugins()
                .sqlMapSelectByPrimaryKeyElementGenerated(answer,
                        introspectedTable)) {
            parentElement.addElement(answer);
        }
	}

}
